package com.hj.system.common.service.impl;

import com.hj.system.base.config.Constant;
import com.hj.system.base.utils.DateUtils;
import com.hj.system.base.utils.FileUtil;
import com.hj.system.common.dao.FileDao;
import com.hj.system.common.domain.FileDO;
import com.hj.system.common.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private FileDao sysFileMapper;
    @Override
    public FileDO get(String id) {
        return sysFileMapper.get(id);
    }

    @Override
    public List<FileDO> list(Map<String, Object> map) {
        return sysFileMapper.list(map);
    }

    @Override
    public int save(FileDO sysFile) {
        return sysFileMapper.save(sysFile);
    }

    @Override
    public int update(FileDO sysFile) {
        return sysFileMapper.update(sysFile);
    }

    @Override
    public int remove(String id) {
        return sysFileMapper.remove(id);
    }

    @Override
    public int batchRemove(String[] ids) {
        return sysFileMapper.batchRemove(ids);
    }

    @Override
    public Boolean isExist(String url) {
        Boolean isExist = false;
        if (!StringUtils.isEmpty(url)) {
            String filePath = url.replace("/files/", "");
            filePath = Constant.uploadBasePath + filePath;
            File file = new File(filePath);
            if (file.exists()) {
                isExist = true;
            }
        }
        return isExist;
    }

    @Override
    @Transactional
    public void saveBatch(List<FileDO> fileDOs) {
        for (FileDO fileDO : fileDOs) {
            save(fileDO);
        }
    }

    @Override
    public List<FileDO> getFileByKey(String key) {
        return sysFileMapper.getByBusinessId(key);
    }

    @Override
    public int batchRemoveByDo(List<FileDO> fileDOs) {
        int i = 0;
        for (FileDO fileDO : fileDOs) {
            i += sysFileMapper.removeByDo(fileDO);
        }
        return i;
    }

    @Override
    public int removeByFileOwner(String fileOwner) {
        return sysFileMapper.removeByFileOwner(fileOwner);
    }

    /**
     * 组装fileDO，并上传至文件服务器
     *
     * @param fileOwner
     * @param files
     * @param base_path
     * @param userId
     * @param userName
     * @return fileDOs
     * @throws IOException
     */
    private List<FileDO> packAndUpload(String fileOwner, MultipartFile[] files, String base_path, String userId, String userName) throws IOException {
        List<FileDO> fileDOs = new ArrayList<>();
        for (MultipartFile file : files) {
            FileDO fileDO = new FileDO();
            fileDO.setFileOwner(fileOwner);
            String originName = file.getOriginalFilename();
            long fileSize = file.getSize();
            fileDO.setFileSize(String.valueOf(fileSize));
            fileDO.setFileName(originName);
            fileDO.setType(originName.substring(originName.lastIndexOf(".")));
            InputStream inputStream = file.getInputStream();
            String url = DateUtils.format() + File.separator + fileOwner;
            String file_path = base_path + File.separator + url;
            fileDO.setUrl(url + File.separator + originName);
            FileUtil.uploadFile(inputStream, file_path, originName);
            fileDO.setCreateUserId(userId);
            fileDO.setCreateUserName(userName);
            fileDOs.add(fileDO);
        }
        return fileDOs;
    }


}
